package com.rbac.common;

import java.time.Duration;

public enum RedisKey {
	// 验证码：5分钟
	CAPTCHA_CODE("rbac:%s:captcha:%s", Duration.ofMinutes(5), "图形验证码"),

	// JWT 黑名单：TTL=token 剩余时长（默认给0，调用方自定）
	TOKEN_BLACKLIST("rbac:%s:jwt:blacklist:%s", Duration.ZERO, "JWT黑名单，值为原因/时间"),

	// 单点登录/在线用户与映射：TTL=token 时长
	ONLINE_USER("rbac:%s:online:%s", Duration.ZERO, "在线用户信息"),
	TOKEN_TO_USER("rbac:%s:token:uid:%s", Duration.ZERO, "token到userId映射"),

	// 用户会话与快照：TTL=token 时长
	USER_SESSION("rbac:%s:session:user:%s", Duration.ZERO, "用户会话快照"),

	// 用户权限/菜单/角色缓存：10-30分钟
	USER_PERMISSIONS("rbac:%s:perms:user:%s", Duration.ofMinutes(30), "用户权限列表"),
	USER_MENUS("rbac:%s:menus:user:%s", Duration.ofMinutes(30), "用户菜单树"),
	USER_ROLES("rbac:%s:roles:user:%s", Duration.ofMinutes(30), "用户角色名列表"),

	// 仪表盘统计：60秒
	DASHBOARD_STATS("rbac:%s:stats:dashboard", Duration.ofSeconds(60), "仪表盘统计缓存");

	private final String pattern;
	private final Duration defaultTtl;
	private final String description;

	RedisKey(String pattern, Duration defaultTtl, String description) {
		this.pattern = pattern;
		this.defaultTtl = defaultTtl;
		this.description = description;
	}

	public String build(String env, Object... args) {
		Object[] keyArgs = new Object[1 + (args == null ? 0 : args.length)];
		keyArgs[0] = env == null ? "dev" : env;
		if (args != null && args.length > 0) {
			System.arraycopy(args, 0, keyArgs, 1, args.length);
		}
		return String.format(this.pattern, keyArgs);
	}

	public Duration ttl() {
		return defaultTtl;
	}

	public String getPattern() {
		return pattern;
	}

	public String getDescription() {
		return description;
	}
} 